package com.lhy.skywalking;

import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.Origin;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.concurrent.Callable;

public class RequestTimeInterceptor {

    @RuntimeType
    public static Object interceptor(@Origin Method method, @AllArguments Object[] args, @SuperCall Callable<?> callable) throws Exception {
        try {
            System.out.println("方法执行前");
            System.out.println("被拦截方法：" + method.getName());
            System.out.println("方法执行前：" + Arrays.asList(args));
            long start = System.currentTimeMillis();
            // 执行方法
            Object call = callable.call();
            long time = System.currentTimeMillis() - start;
            System.out.println("方法返回值：" + call);
            System.out.println("方法执行时间：" + time + "ms");
            return call;
        } catch (Exception e) {
            System.out.println("方法执行出现异常：" + e.getMessage());
        } finally {
            System.out.println("方法执行完毕");
        }
        return null;
    }
}
